﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.WebApp.Models;
using XSTDZ.ERP.WebApp.Models.Ado;

namespace XSTDZ.ERP.WebApp.Controllers
{
    /// <summary>
    /// 汇款记录
    /// </summary>
    [NoAuthorizated]
    public class RemittanceRecordController : Controller
    {
        //
        // GET: /UsualFinanceC/
        private readonly IUsualFinanceService workGroupServiceImpl = ServiceLocator.Instance.GetService<IUsualFinanceService>();
        public ActionResult Index()
        {
            ViewBag.orgCode = SystemConfig.ChengDuCode;
            return View();
        }

        #region 查看详细
        [NoAuthorizated("Detail")]
        public ActionResult Detail(Guid id)
        {
            var billModel = workGroupServiceImpl.GetRemittanceRecord(id);
            return View(billModel);
        }
        #endregion
        #region 新增汇款记录
        [NoAuthorizated("Add")]
        public ActionResult Add(string orgCode,Guid? billId)
        {
            var wg = new RemittanceRecordDto();
            wg.RemittanceTime = DateTime.Today;
            if (billId != null && !Guid.Empty.Equals(billId))
            {
    
                wg = workGroupServiceImpl.GetRemittanceRecord(billId.Value);
            }
            ViewBag.OrgCode = orgCode;
            return View(wg);
        }
        [HttpPost]
        [NoAuthorizated("Add")]
        public ActionResult Add(RemittanceRecordDto workGroup)
        {

            //var type = this.HttpContext.Request.Form["ProjectType"];
            //workGroup.ProjectType = type == "0" ? 0 : 1;
            var isCommit= this.HttpContext.Request.Form["HidIsCommit"];
            #region 组织机构
            var orgCode = this.HttpContext.Request.Form["HidOrgCode"];
            IOrgService orgServiceImpl = ServiceLocator.Instance.GetService<IOrgService>();
            workGroup.OrgID = orgServiceImpl.GetOrg(orgCode).Id;

            ViewBag.OrgCode = orgCode;

            var csID = Request.Form["RemittancePeoperName_val"];
            if (!string.IsNullOrEmpty(csID))
            {
                workGroup.RemittancePeoperId = Guid.Parse(csID);
            }
            
            #endregion

            if (ModelState.IsValid)
            {
                var uid = UserModel.GetLoginUserId(this.HttpContext);
                if (!workGroup.Id.Equals(Guid.Empty))
                {
                    if (isCommit == "1")
                    {
                        if (workGroup.BillState == 1)
                        {
                            ViewBag.PostState = -1;
                            ModelState.AddModelError("error", "已提交，不能修改！");
                            return View(workGroup);
                        }
                        else
                        {
                            workGroup.BillState = 1;
                        }
                    }
                    workGroup.UpdateID = uid;
                    this.workGroupServiceImpl.UpdateRemittanceRecord(workGroup);
                }
                else
                {
                    workGroup.CreateID = uid;
                    workGroup.CreateTime = DateTime.Now;
                    if (isCommit == "1")
                    {
                        workGroup.BillState = 1;
                    }
                    this.workGroupServiceImpl.AddRemittanceRecord(workGroup);
                }
                ViewBag.PostState = 9;
            }

            return View(workGroup);
        }

        public ActionResult Delete(Guid billId)
        {
            workGroupServiceImpl.DeleteRemittanceRecord(billId);
            return Content("9");
        }
        #endregion
        #region 汇款记录确认
        public ActionResult ConfirmIndex()
        {
            return View();
        }
         [NoAuthorizated("Confirm")]
        public ActionResult Confirm(Guid billId)
        {
            
            var wg = workGroupServiceImpl.GetRemittanceRecord(billId);
            return View(wg);
        }
         [NoAuthorizated("ConfirmPost")]
         public ActionResult ConfirmPost(Guid billId, bool isConfirmOk)
         {
             var uid = UserModel.GetLoginUserId(this.HttpContext);
             var wg = workGroupServiceImpl.ConfirmRemittanceRecord(billId, uid, isConfirmOk);
             var result=wg.Equals(billId)?"9":"0";
             return Content(result);
         }
        #endregion
        #region 获得汇款记录列表
        [NoAuthorizated("GetJsonOfBills")]
        public ActionResult GetJsonOfBills()
        {
            #region EF的查询，效率低下，故暂时放弃不用
            /*
            var workGroupService = workGroupServiceImpl.GetUsualFinances();
            if (workGroupService != null && workGroupService.Count() != 0)
            {
                workGroupService.Select(t => { t.ProjectTypeName = t.ProjectType == 0 ? "收入" : "支出"; return t; });
                var target = Newtonsoft.Json.JsonConvert.SerializeObject(workGroupService);
                target = "{\"Rows\":" + target + ",\"Total\":\"" + workGroupService.Count() + "\"}";
                return Content(target);
            }
            return View();
            */
            #endregion
            return Content(new BLLQuery().GetRemittanceRecords(this.HttpContext));
        }
        #endregion
        #region 深圳业务
        
         public ActionResult SZIndex()
         {
             ViewBag.orgCode = SystemConfig.ShengZhenCode;
             return View("Index");
         }
        #endregion
        #region 收入支出
         private void BindProjectType(string type)
         {
             var list = new List<SelectListItem> { 
                 new SelectListItem{ Text="收入", Value="0"},
                 new SelectListItem{ Text="支出", Value="1"}
            };
             if (!string.IsNullOrEmpty(type))
             {
                 list.ForEach(t => { if (t.Value.ToString() == type) { t.Selected = true; } });
             }
             ViewBag.ProjectType = list;
         }
         #endregion
    }
}
